package com.example.huainanlive.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.ActivityUtils
import com.demon.androidbasic.action.HandlerAction
import com.demon.androidbasic.action.NoDoubleClickAction
import com.demon.androidbasic.base.BaseLifecycleFragment
import com.demon.androidbasic.dialog.BaseDialog
import com.demon.androidbasic.dialog.WaitDialog
import com.demon.androidbasic.toast.shortToast
import com.demon.net.AppResponse
import com.hjq.http.listener.OnHttpListener
import okhttp3.Call


abstract class BaseBindingViewFragment<T : ViewBinding>(id: Int) : BaseLifecycleFragment(id), HandlerAction,
    NoDoubleClickAction {
    private var _binding: T? = null
    val binding get() = _binding!!
    override var clickTime = 0L
    override fun <V : View> findViewById(id: Int): V {
        return binding.root.findViewById(id)
    }

    /**
     * 加载对话框
     */
    private var mDialog: BaseDialog? = null

    /**
     * 对话框数量
     */
    private var mDialogTotal = 0

    /**
     * 当前加载对话框是否在显示中
     */
    val isShowDialog: Boolean
        get() = mDialog != null && mDialog!!.isShowing

    /**
     * 显示加载对话框
     */
    fun showDialog() {
        mDialogTotal++
        postDelayed({
            if (mDialogTotal > 0 && activity?.isFinishing != true) {
                if (mDialog == null) {
                    mDialog = WaitDialog.Builder(ActivityUtils.getTopActivity())
                        .setCancelable(false)
                        .create()
                }
                if (!mDialog!!.isShowing) {
                    mDialog!!.show()
                }
            }
        }, 300)
    }

    /**
     * 隐藏加载对话框
     */
    fun hideDialog() {
        if (mDialogTotal > 0) {
            mDialogTotal--
        }
        if (mDialogTotal == 0 && mDialog != null && mDialog!!.isShowing && activity?.isFinishing != true) {
            mDialog!!.dismiss()
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = initBinding(inflater)
        return _binding?.root
    }


    protected val onHttpListener: OnHttpListener<AppResponse<*>?>
        get() {
            return object : OnHttpListener<AppResponse<*>?> {

                override fun onStart(call: Call) {
                    //showDialog()
                }

                override fun onSucceed(result: AppResponse<*>?) {
                    if (result is AppResponse<*> && "成功" != result.getResponseMessage()) {
                        shortToast(result.getResponseMessage())
                    }
                }

                override fun onFail(e: Exception?) {
                    shortToast(e?.message)
                    hideDialog()
                }

                override fun onEnd(call: Call) {
                    hideDialog()
                }
            }
        }

    override fun onDestroy() {


        super.onDestroy()
        _binding = null
        if (isShowDialog) {
            hideDialog()
        }
        mDialog = null

    }

    abstract fun initBinding(layoutInflater: LayoutInflater): T
}